﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Windows.Forms.ComponentModel;
//using System.Net.NetworkInformation;

namespace GuestBook_Lib
{
    public class Survey
    {
        Data dat;
        public Survey(string [] Q)
        {
            Question = Q;
            dat = new Data();
            //Load_Mem(); //Do NOT ever call Directly from HERE!!!!
            string path = Save();
            Load_File(path);
        }
        public Survey(ListBox T)
        {
            l = T;
        }
        private void Load_Mem()
        {
            l.Items.Clear();
            try
            {
                l.Items.AddRange(Question);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                MessageBox.Show("Load to memory failed.");
            }
        }
        internal string Save()
        {
            SaveFileDialog s = new SaveFileDialog();
            s.Filter = "Survey Files (*.surv) |*.surv";
            s.DefaultExt = ".surv";
            s.ShowDialog();
            string path = "";
            path = s.FileName;
            if (path != "" && path != "s")
            {
                Save(path);
                return path;
            }
            return Application.StartupPath + "\\Sample.surv";
        }
        public void Save(string path)
        {
            try
            {
                System.IO.File.WriteAllLines(path, Question);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                MessageBox.Show("Failed save to string.");
            }
        }
        public void Null_Test()
        {
            //Tests to see if Survey types are started properly
        }
        public void Load_File(string path)
        {
            try
            {
                Question = System.IO.File.ReadAllLines(path);
                Load_Mem();
            }
            catch (Exception ex)
            {
                Question = new string[0];
                MessageBox.Show(ex.Message);
                MessageBox.Show("For security of this application, survey questions loaded into memory have been cleared.\nWe apologize for the inconvinence.");
            }
            
        }
        public void Call_Maker()
        {
            try
            {
                //MessageBox.Show(Application.StartupPath);
                //Exception Te = new Exception();
                //throw Te;
                System.Diagnostics.Process.Start(Application.StartupPath + "\\Survey2.exe");
                
            }
            catch (Exception ex)
            {
                 
                 var Result = MessageBox.Show(ex.Message + "\nDo you wish to download it now?", "Survey Maker", MessageBoxButtons.YesNo, MessageBoxIcon.Error);
                 if (Result == DialogResult.Yes)
                 {
                     if (NetworkInterface.GetIsNetworkAvailable() == true)
                     {
                         WebClient web = new WebClient();
                         byte[] array = web.DownloadData("http://guestbook-for-frc-guests.googlecode.com/files/Survey2.exe");
                         System.IO.File.WriteAllBytes(Application.StartupPath + "\\Survey2.exe", array);
                         if (Preferences.Default.Show_Website == true)
                         {
                             //MessageBox.Show(Preferences.Default.Show_Website.ToString());
                             System.Diagnostics.Process.Start("http://gb2.codeplex.com/");
                         }
                     }
                     else
                     {
                         MessageBox.Show("Error: Network is NOT up, please try again later.");
                     }
                 }
            }
        }
        private static ListBox l;
        private static string [] Question;
    }
    public class Data //Data class, keeps track of statistics
    {
        public Data()
        {
            dataP = new string[1];
            Ft = 0;
        }
        public Data(string Np)
        {
            if (dataP.Length <= Ft)
            {
                string[] temp = dataP;
                dataP = new string[temp.Length + 1];
                temp.CopyTo(dataP, 0);
                dataP[dataP.Length -1] = Np;
                Ft++;
            }
            else
            {
                dataP[Ft -1 ] = Np;
                Ft++;
            }
        }
        internal Data(int Starting)
        {
            dataP = new string[Starting + 1];
        }
        private static string[] dataP; //Data Points
        private int Ft = 0; //Where is the array filled to?
    }
}
